package com.oracle.oa.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.oracle.oa.dao.FileinfoDao;
import com.oracle.oa.entity.Fileinfo;

public class FileinfoDaoImpl extends BaseDaoImpl implements FileinfoDao {

	
	public void toRecycleBin(long fileid,long ifdelete) {
		String hql="update Fileinfo f set f.ifdelete="+ifdelete+" where f.fileid="+fileid;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Query query=session.createQuery(hql);
		query.executeUpdate();
		session.close();
	}
	
	
	public List<Fileinfo> findFileChild(long fileid) {
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		List<Fileinfo> list = new ArrayList<Fileinfo>();
		Criteria c = session.createCriteria(Fileinfo.class);
		c.add(Restrictions.eq("parentid", fileid));
		c.add(Restrictions.eq("ifdelete", 0l));
		list = c.list();
		session.close();
		return list;
	}

	
	public List<Fileinfo> findFile(String filename, String owner,
			Date begintime, Date endtime, String whiletime) {
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		List<Fileinfo> list = new ArrayList<Fileinfo>();
		int day = Calendar.DAY_OF_WEEK;
		if (day == 0)
			day = 7;
		String hql=" from Fileinfo f where f.ifdelete=0l ";
		String hql1 = " f.filename like '%" + filename + "%'  ";//根据文件名查询
		String hql2=" f.userinfo.username like '%" + owner + "%' "; //根据文件创建者查询
		String hql3=" f.createdate>=sysdate-1 ";//查询本日上传的文件
		String hql4=" to_char(f.createdate,'yyyy-mm')=to_char(sysdate,'yyyy-mm') ";//查询本月的
		String hql5=" f.createdate between sysdate-" + day + " and sysdate+" + (7 - day) +" ";//查询本周的
		String hql6=" f.createdate between ? and ? ";
		boolean flag=false;
		boolean whiletimeIS=false;
		boolean timeIS=false;
		Query query = null;
		if(filename!=null){
			flag=true;
		    hql=hql+"and"+hql1;
		}
		if(owner!=null){
			if(flag){
				hql=hql+"and"+hql2;
			}else{
				hql=hql+hql2;
				flag=true;
			}
		}
		if(whiletime!=null){
			whiletimeIS=true;
			if(flag){
				hql=hql+"and";
			}
			if (whiletime.equals("1")) {
				hql=hql+hql3;
			} else if (whiletime.equals("2")) {
				hql=hql+hql4;
			} else {
				hql=hql+hql5;
			}
			flag=true;
		}
		
		if(begintime!=null&&endtime!=null){
			if(!whiletimeIS){
			   if(flag){
				   hql=hql+"and"+hql6;
			   }else{
				   hql=hql+hql6;
				   flag=true;
			   }
			   timeIS=true;
			}
		}
		query = session.createQuery(hql);
		if(timeIS){
			query.setDate(0, begintime);
			query.setDate(1, endtime);
		}
		list = query.list();
		session.close();
		return list;
		
	}

	
	public List<Fileinfo> findRecycleBin() {
		List<Fileinfo> list=null;
		
		return list;
	}

}
